package cn.felord.boot;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }


    public static boolean checkSymmetricTree(TreeNode root) {
        return check(root, root);
    }


    /***
     * 递归
     * 判断二叉树是否左右对称
     * 给定1个二叉树 ，检查它是否是镜像对称的。
     * 例如 ，二叉树 [1,2,2,3,4,4,3] 是对称的。
     *             1
     *            / \
     *          2     2
     *         / \   / \
     *       3   4  4   3
     * 3.但是下面这个 [1,2,2,null,3,null,3]则不是镜像对称的:
     *            1
     *           / \
     *         2     2
     *         \     \
     *          3      3
     * @param l
     * @param r
     * @return
     */
    public static boolean check(TreeNode l, TreeNode r) {
        if (l == null && r == null) {
            return true;
        }
        if (l == null || r == null) {
            return false;
        }
        return l.val == r.val && check(l.left, r.right) && check(l.right, r.left);
    }



    /***
     *  借助栈的迭代方法
     * 5.迭代法写二叉树中序遍历
     * 输入：
     *      1
     *     / \
     *   3     6
     *  / \   / \
     * 2   4 5   7
     * @param root
     */
    public static List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> ans = new ArrayList<>();
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null || !stack.isEmpty()) {
            //先遍历左子树放入栈里面
            while (cur != null) {
                stack.push(cur);
                //把当前节点指针指向左子节点
                cur = cur.left;
            }
            //先取出栈的顶部元素
            cur = stack.pop();
            //把当前元素的值存入list里面
            ans.add(cur.val);
            //把当前节点指针指向右子节点
            cur = cur.right;
        }
        return ans;
    }


    /***
     *  [1,2,2,3,4,4,3]
     * @param args
     */
    public static void main(String[] args) {
        // [1,2,2,3,4,4,3]
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);

        System.out.println(checkSymmetricTree(root));

        // [1,2,2,null,3,null,3]
        TreeNode root1 = new TreeNode(1);
        root1.left = new TreeNode(2);
        root1.right = new TreeNode(2);
        root1.left.left = null;
        root1.left.right = new TreeNode(3);
        root1.right.left = null;
        root1.right.right = new TreeNode(3);

        System.out.println(checkSymmetricTree(root1));

        //           1
        //          / \
        //        3     6
        //       / \   / \
        //      2   4 5   7
        TreeNode root2 = new TreeNode(1);
        root2.left = new TreeNode(3);
        root2.right = new TreeNode(6);
        root2.left.left = new TreeNode(2);
        root2.left.right = new TreeNode(4);
        root2.right.left = new TreeNode(5);
        root2.right.right = new TreeNode(7);
        List<Integer> ans = inorderTraversal(root2);
        System.out.println(ans);


        TreeNode root3 = new TreeNode(1);
        root3.left = null;
        root3.right = new TreeNode(2);
        root3.right.left = new TreeNode(3);
        List<Integer> ans1 = inorderTraversal(root3);
        System.out.println(ans1);
    }
}